// Copyright 2011 Google Inc. All Rights Reserved.

/**
 * @fileoverview This module provides methods for displaying results.
 *
 * @author gavaletz@google.com (Eric Gavaletz)
 */


goog.provide('results');


goog.require('goog.dom');


/**
 * Wrapper for adding visualization listners.
 *
 * @param {(google.visualization.Table|
 *          google.visualization.ScatterChart|
 *          google.visualization.PieChart)} chart a chart to work with.
 * @param {string} eventName the name of the event to listen for.
 * @param {function()} callback the function to call.
 */
results.addListener = function(chart, eventName, callback) {
  google.visualization.events.addListener(chart, eventName, callback);
};


/**
 * Testing out the charting tools API.
 * http://code.google.com/apis/chart/interactive/docs/index.html
 *
 * http://code.google.com/apis/chart/interactive/docs/gallery/scatterchart.html
 *
 * @param {Object.<string, Array>} dataObj data suitable for charting.
 * @param {string} title the title to use in the chart.
 * @param {!Element} parentDiv the location to render the chart.
 *
 * @return {google.visualization.ScatterChart} The table we just made.
 */
results.scatterChart = function(dataObj, title, parentDiv) {
  var data = new google.visualization.DataTable(dataObj);
  var chart = new google.visualization.ScatterChart(parentDiv);
  var options = {'curveType': 'none', 'lineWidth': 1, 'pointSize': 0,
    'title': title, 'vAxis': {'title': dataObj['cols'][1]['label']},
    'hAxis': {'title': dataObj['cols'][0]['label']},
    'legend': {'position': 'none'}, 'titlePosition': 'in'};
  chart.draw(data, options);
  return chart;
};


/**
 * http://code.google.com/apis/chart/interactive/docs/gallery/table.html
 *
 * @param {Object.<string, Array>} dataObj data suitable for charting.
 * @param {!Element} parentDiv the location to render the chart.
 *
 * @return {google.visualization.Table} The table we just made.
 */
results.table = function(dataObj, parentDiv) {
  var data = new google.visualization.DataTable(dataObj);
  var chart = new google.visualization.Table(parentDiv);
  var options = {'allowHtml': true, 'page': 'enable', 'pageSize': 30};
  chart.draw(data, options);
  return chart;
};


/**
 * http://code.google.com/apis/chart/interactive/docs/gallery/piechart.html
 *
 * @param {Object.<string, Array>} dataObj data suitable for charting.
 * @param {string} title the title to use in the chart.
 * @param {!Element} parentDiv the location to render the chart.
 *
 * @return {google.visualization.PieChart} The chart we just made.
 */
results.pieChart = function(dataObj, title, parentDiv) {
  var data = new google.visualization.DataTable(dataObj);
  var chart = new google.visualization.PieChart(parentDiv);
  var options = {'title': title};
  chart.draw(data, options);
  return chart;
};


/**
 * Builds an object that we can use to create charts and tables.
 *
 * http://code.google.com/apis/chart/interactive/docs/reference.html#dataparam
 *
 * @param {?Array.<number>} x optional values.
 * @param {!Array.<number>} y necessary values.
 * @param {string} xLabel the lable for the x axis.
 * @param {string} yLabel the lable for the y axis.
 * @param {string} xType the type for the x axis.
 * @param {string} yType the type for the y axis.
 *
 * @return {Object.<string, Array>} the data object suitable for charting.
 */
results.dataToObj = function(x, y, xLabel, yLabel, xType, yType) {
  var dataObj = {'cols': [], 'rows': []};
  dataObj['cols'][0] = {'id': 'x', 'label': xLabel, 'type': xType};
  dataObj['cols'][1] = {'id': 'y', 'label': yLabel, 'type': yType};

  // if there is not an array for x then us a count.
  // otherwise use the smaller length to avoid index errors
  if (x == null) {
    for (i = 0; i < y.length; i++) {
      dataObj['rows'][i] = {'c': [{'v': i}, {'v': y[i]}]};
    }
  }
  else {
    for (i = 0; i < Math.min(x.length, y.length); i++) {
      dataObj['rows'][i] = {'c': [{'v': x[i]}, {'v': y[i]}]};
    }
  }
  return dataObj;
};


/**
 * Builds an object that we can use to create charts and tables.
 *
 * http://code.google.com/apis/chart/interactive/docs/reference.html#dataparam
 *
 * @param {Object.<string, {*}>} records to build the dataObj.
 *
 * @return {Object.<string, Array>} the data object suitable for charting.
 */
results.recordsToObj = function(records) {
  var dataObj = {'cols': [], 'rows': []};
  var keys = Object.keys(records[0]);
  var max_i = keys.length;
  var max_j = records.length;
  for (i = 0; i < max_i; i++) {
    dataObj['cols'][i] = {'id': 'c' + i, 'label': keys[i],
      'type': typeof records[0][keys[i]]};
  }
  for (j = 0; j < max_j; j++) {
    dataObj['rows'][j] = {'c': []};
    for (i = 0; i < max_i; i++) {
      dataObj['rows'][j]['c'].push({'v': records[j][keys[i]]});
    }
  }
  return dataObj;
};
